package com.macro.mall.tiny.modules.foster.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.macro.mall.tiny.common.api.CommonPage;
import com.macro.mall.tiny.common.api.CommonResult;
import com.macro.mall.tiny.modules.foster.model.AlipayOrder;
import com.macro.mall.tiny.modules.foster.model.dto.ItemDTO;
import com.macro.mall.tiny.modules.foster.model.dto.PetAndTimeDTO;
import com.macro.mall.tiny.modules.foster.service.AlipayOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 支付宝支付订单表 前端控制器
 * </p>
 *
 * @author zhuyi
 * @since 2024-04-02
 */
@RestController
@Api(tags = "AlipayOrderController")
@Tag(name = "AlipayOrderController", description = "支付宝订单管理")
@RequestMapping("/foster/alipayOrder")
@CrossOrigin
public class AlipayOrderController {
    private static final Logger LOGGER = LoggerFactory.getLogger(AlipayOrderController.class);
    @Autowired
    private AlipayOrderService alipayOrderService;

    /**
     * 分页查询商品列表
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @ApiOperation("分页查询订单列表")
    @PreAuthorize("hasAnyAuthority('user','admin')")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<AlipayOrder>> list(@RequestParam(value = "pageNum", defaultValue = "1")
                                                      @ApiParam("页码") Integer pageNum,
                                                      @RequestParam(value = "pageSize", defaultValue = "5")
                                                      @ApiParam("每页数量") Integer pageSize,
                                                      @RequestParam(value = "type", required = false)
                                                      @ApiParam("订单类型,可不传获取全部") Integer type,
                                                      @RequestParam(value = "userId", required = false)
                                                      @ApiParam("用户id,可不传获取全部") Long userId) {

        Page<AlipayOrder> page = new Page<>(pageNum, pageSize);
        Page<AlipayOrder> pageResult = alipayOrderService.pageItem(page, type, userId);
        LOGGER.info("订单列表查询状态：查询成功");
        return CommonResult.success(CommonPage.restPage(pageResult));
    }

    @ApiOperation("创建寄养宠物预约订单")
    @RequestMapping(value = "/createAppointOrder", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<AlipayOrder> createAppointOrder(@RequestBody PetAndTimeDTO timeDTO) {
        AlipayOrder order = alipayOrderService.createAppointOrder(timeDTO);
        if (order == null) {
            return CommonResult.failed("时间错误");
        }
        LOGGER.info("创建寄养宠物预约订单成功");
        return CommonResult.success(order);
    }

    @ApiOperation("创建商品订单")
    @RequestMapping(value = "/createItemOrder", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<AlipayOrder> createItemOrder(@RequestBody ItemDTO itemDTO) {
        AlipayOrder order = alipayOrderService.createItemOrder(itemDTO);
        if (order == null) {
            LOGGER.info("库存不足");
            return CommonResult.failed("库存不足");
        }
        LOGGER.info("创建商品订单成功");
        return CommonResult.success(order);
    }

    @ApiOperation("修改订单,只改订单状态")
    @RequestMapping(value = "/updateOrder", method = RequestMethod.PUT)
    @ResponseBody
    public CommonResult updateOrder(@RequestBody AlipayOrder alipayOrder) {
        alipayOrderService.updateById(alipayOrder);
        LOGGER.info("修改订单成功");
        return CommonResult.success("修改订单成功");
    }
    @ApiOperation("删除订单")
    @RequestMapping(value = "/deleteOrder/{orderId}", method = RequestMethod.DELETE)
    @ResponseBody
    public CommonResult deleteOrder(@PathVariable String orderId) {
        alipayOrderService.removeById(orderId);
    return CommonResult.success("删除订单成功");
    }
    @ApiOperation("查询单个订单")
    @RequestMapping(value = "/info/{orderId}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<AlipayOrder> info(@PathVariable String orderId) {
        AlipayOrder order = alipayOrderService.getById(orderId);
        LOGGER.info("查询单个订单成功");
        return CommonResult.success(order);
    }
}
